﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;
using SqlMiM.Common.Framework.UserInterfaces;
using SqlMiM.Common.Framework;
using SqlMiM.Common.Framework.Configuration;

namespace SqlMiM.Common.Themes
{
    public class ThemesServices
    {
        #region Singleton

        private static ThemesServices current;

        public static ThemesServices Current
        {
            get
            {
                if (current == null)
                {
                    current = new ThemesServices();
                }

                return current;
            }
        }

        #endregion

        /// <summary>
        /// Contains all Configs Properties
        /// </summary>
        private ConfigurationObject configurationObject;

        /// <summary>
        /// All active Themes
        /// </summary>
        public List<Theme> Themes { get; set; }

        /// <summary>
        /// Current activated Theme
        /// </summary>
        public Theme ActiveTheme { get; private set; }

        /// <summary>
        /// Constructor
        /// </summary>
        public ThemesServices()
        {
            // Create List of Themes
            this.Themes = new List<Theme>();
        }

        /// <summary>
        /// Load all themes
        /// </summary>
        public void Load()
        {
            // Clear List
            this.Themes.Clear();

            // Add the only two themes existing :)
            Theme lightTheme = new Theme("Light Theme", "LightBrushes", true);
            Theme darkTheme = new Theme("Dark Theme", "DarkBrushes", true);

            this.Themes.Add(lightTheme);
            this.Themes.Add(darkTheme);

            // Get the Configuration Object
            this.configurationObject = Context.Current.Configuration[ThemeOptionsPage.NAME];

            // Get the current Theme
            String currentTheme = this.configurationObject.GetProperty(ThemeOptionsPage.ACTIVE_THEME_PROPERTY, ThemeOptionsPage.ACTIVE_THEME_DEFAULT_VALUE) as String;

            // Active Theme
            this.UpdateActiveTheme(currentTheme);
        }

        /// <summary>
        /// Update a new theme, if exists
        /// </summary>
        public void UpdateActiveTheme(String themeName)
        {
            Theme newTheme = null;

            newTheme = this.Themes.Find(t => t.Name.Equals(themeName, StringComparison.InvariantCultureIgnoreCase));

            if (newTheme == null) return;
            
            this.EnsureMergedDictionaries();

            this.ActiveTheme = newTheme;

            if (Application.Current.Resources.MergedDictionaries.Count == 0)
                Application.Current.Resources.MergedDictionaries.Add(newTheme.ResourceDictionary);
            else
                Application.Current.Resources.MergedDictionaries[0] = newTheme.ResourceDictionary;

        }


        /// <summary>
        /// Check Ressources
        /// </summary>
        internal void EnsureMergedDictionaries()
        {
            if (Application.Current.Resources == null)
            {
                Application.Current.Resources = new ResourceDictionary();
            }
        }
    }

    
}
